package com.glory.erp.auth.role.action;

import java.util.List;

import com.glory.erp.auth.menu.bean.Menu;
import com.glory.erp.auth.menu.service.MenuService;
import com.glory.erp.auth.resources.bean.Resources;
import com.glory.erp.auth.resources.service.ResourcesService;
import com.glory.erp.auth.role.service.RoleService;
import com.glory.erp.auth.role.bean.Role;
import com.glory.erp.auth.role.bean.RoleQuery;
import com.glory.erp.auth.base.BaseAction;

public class RoleAction extends BaseAction {

	public Role role = new Role();
	public RoleQuery roleQuery = new RoleQuery();
	public Long[] resourcesUuids;
	public Long[] menuUuids;

	private RoleService roleService;
	private ResourcesService resourcesService;
	private MenuService menuService;

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public void setResourcesService(ResourcesService resourcesService) {
		this.resourcesService = resourcesService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * 
	 * @Description: 角色信息展示
	 * @Title: list
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 下午6:29:35
	 * @throws
	 */
	public String list() {
		setDataTotal(roleService.getCount(roleQuery));
		List<Role> roleList = roleService
				.findAll(roleQuery, pageNum, pageCount);
		put("roleList", roleList);
		return LIST;
	}

	/**
	 * 
	 * @Description: 跳转至添加、修改页面
	 * @Title: addView
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 下午6:29:52
	 * @throws
	 */
	public String addView() {
		List<Menu> menuList = menuService.findAll();
		put("menuList", menuList);
		List<Resources> resourcesList = resourcesService.findAll();
		put("resourcesList", resourcesList);
		// 进入修改页面
		if (role.getUuid() != null) {
			// 资源回显
			role = roleService.findById(role.getUuid());
			resourcesUuids = new Long[role.getResources().size()];
			int i = 0;
			for (Resources r : role.getResources()) {
				resourcesUuids[i++] = r.getUuid();
			}
			// 菜单回显
			menuUuids = new Long[role.getMenus().size()];
			i = 0;
			for (Menu m : role.getMenus()) {
				menuUuids[i++] = m.getUuid();
			}
		}
		return ADD;
	}

	/**
	 * 
	 * @Description: 角色添加、修改
	 * @Title: saveAndUpdate
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 下午6:30:11
	 * @throws
	 */
	public String saveAndUpdate() {
		if (role.getUuid() == null) {
			roleService.save(role, resourcesUuids, menuUuids);
		} else {
			roleService.update(role, resourcesUuids, menuUuids);
		}
		return TO_LIST;
	}

	/**
	 * 
	 * @Description: 角色删除
	 * @Title: delete
	 * @Author: 樊双峰
	 * @Param: @return
	 * @return: String
	 * @Date: 2018年5月20日 下午6:30:28
	 * @throws
	 */
	public String delete() {
		roleService.delete(role);
		return TO_LIST;
	}
}